package com.yicong.ants.ui.common;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.cchao.simplelib.core.ImageLoader;
import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.ui.activity.BaseActivity;
import com.github.chrisbanes.photoview.PhotoView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.utils.L;
import com.yicong.ants.utils.StatusBarUtil;
import com.yicong.ants.view.dialog.SaveImageDialog;

import java.util.List;

/**
 * 图片浏览页
 *
 * @author cch
 * @version 2020/5/19
 */
public class ImageViewPagerActivity extends BaseActivity {
    private ViewPager viewPager;
    private MyViewPagerAdapter myViewPagerAdapter;
    private List<String> mStringList;
    TextView mIndicator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_viewpage_activity);
        StatusBarUtil.setStatusImmerAndTextWhite(getWindow());
        mStringList = JsonHelper.toList(getIntent().getStringExtra(Constants.Extra.JSON), String.class);
        initView();
        initData();
    }

    private void initData() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float v, int i1) {
                mIndicator.setText((position + 1) + "/" + mStringList.size());
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ((Activity) mContext).overridePendingTransition(0, R.anim.zoom_exit);
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        myViewPagerAdapter = new MyViewPagerAdapter(this, mStringList);
        viewPager.setAdapter(myViewPagerAdapter);
        mIndicator = findViewById(R.id.indicator);
        int index = getIntent().getIntExtra(Constants.Extra.Index, 0);
        viewPager.setCurrentItem(index);

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    static class MyViewPagerAdapter extends PagerAdapter {
        List<String> imgList;
        ImageViewPagerActivity context;

        public MyViewPagerAdapter(ImageViewPagerActivity context, List<String> imgList) {
            this.imgList = imgList;
            this.context = context;
        }


        @Override
        public int getCount() {
            return imgList.size();
        }

        //指定复用的判断逻辑，固定写法：view == object
        @Override
        public boolean isViewFromObject(View view, Object object) {
            //当创建新的条目，又反回来，判断view是否可以被复用(即是否存在)
            return view == object;
        }

        //返回要显示的条目内容
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //container  容器  相当于用来存放imageView
            L.d("instantiateItem", "要显示的条目:" + position);
            PhotoView photoView = new PhotoView(context);
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            ImageLoader.loadImage(photoView, imgList.get(position));
            container.addView(photoView);
            photoView.setOnLongClickListener(v -> {
                showSaveImgDialog(photoView,context);
                return false;
            });
            photoView.setOnClickListener(v -> context.onBackPressed());
            return photoView;
        }

        private static void showSaveImgDialog(PhotoView photoView,ImageViewPagerActivity context) {

            SaveImageDialog.newInstance()
                    .setPhotoView(photoView)
                    .show(context.getSupportFragmentManager());
        }

        //销毁条目
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //object:刚才创建的对象，即要销毁的对象
            container.removeView((View) object);
        }
    }
}
